<?php

namespace app\interactive\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

use app\interactive\model\Question as QuestionModel;
use app\interactive\model\Active as ActiveModel;
use think\helper\Time;

class Question extends Admin
{
    public function Index()
    {

        $map = $this->getMap();

        $activeData = ActiveModel::column('activeName', 'id');

        $questionData = QuestionModel::where($map)->order('id asc')->paginate();

        return ZBuilder::make('table')
            ->setTableName('question')
            ->addColumns([
                ['id', 'ID'],
                ['topic', '题目'],
                ['optionA', '选项A'],
                ['optionB', '选项B'],
                ['optionC', '选项C'],
                ['optionD', '选项D'],
                ['correct', '答案'],
                ['activeId', '所属活动', $activeData],
                ['addTime', '添加时间', 'datetime', '', 'Y/m/d H:i:s'],
                ['updateTime', '更新时间', 'datetime', '', 'Y/m/d H:i:s'],
                ['right_button', '操作', 'btn']
            ])
            ->setRowList($questionData)
            ->addTopButton('add', [], true)
            ->addTopButton('delete')
            ->addRightButton('edit', [], true)
            ->addRightButton('delete')
            ->addTopSelect('activeId','所有活动',$activeData)
            ->fetch();
    }
    //新增
    public function add()
    {
        $activeData = ActiveModel::column('activeName', 'id');

        if ($this->request->isPost()) {
            $newData = $this->request->post();

            // 基础验证
            $result = $this->validate($newData, 'Question');

            // 验证失败 输出错误信息
            if (true !== $result) {
                $this->error($result);
            }

            // 答案必须是选项之一
            if (!in_array($newData['correct'], ['A', 'B', 'C', 'D'])) {
                $this->error('答案字段必须是A, B, C或D');
            }

            // 验证活动ID是否有效
            if (!isset($activeData[$newData['activeId']])) {
                $this->error('无效的活动ID');
            }

            // 验证四个选项是否不同
            $options = [
                $newData['optionA'],
                $newData['optionB'],
                $newData['optionC'],
                $newData['optionD']
            ];
            if (count(array_unique($options)) < 4) {
                $this->error('四个选项不能完全相同');
            }

            $newData['addTime'] = time(); // 使用time()函数获取当前时间戳
            $newData['updateTime'] = time();

            if (QuestionModel::create($newData)) {
                $this->success('新增成功', null, '_parent_reload');
            } else {
                $this->error('新增失败');
            }
        }

        return ZBuilder::make('form')
            ->setPageTitle('添加题目')
            ->addFormItems([
                ['textarea', 'topic', '题目'],
                ['text', 'optionA', '选项A'],
                ['text', 'optionB', '选项B'],
                ['text', 'optionC', '选项C'],
                ['text', 'optionD', '选项D'],
                ['text', 'correct', '答案'],
                ['select', 'activeId', '题目所属', '请选择这个题目所属的活动', $activeData],
            ])
            ->layout(['optionA' => 6, 'optionB' => 6, 'optionC' => 6, 'optionD' => 6, 'correct' => 6, 'activeId' => 6])
            ->fetch();
    }
    //编辑
    public function edit($id = '')
    {
        $activeData = ActiveModel::column('activeName', 'id');

        if ($this->request->isPost()) {
            $newData = $this->request->post();

            // 基础验证
            $result = $this->validate($newData, 'Question');

            // 验证失败 输出错误信息
            if (true !== $result) {
                $this->error($result);
            }

            // 答案必须是选项之一
            if (!in_array($newData['correct'], ['A', 'B', 'C', 'D'])) {
                $this->error('答案字段必须是A, B, C或D');
            }

            // 验证活动ID是否有效
            if (!isset($activeData[$newData['activeId']])) {
                $this->error('无效的活动ID');
            }

            // 验证四个选项是否不同
            $options = [
                $newData['optionA'],
                $newData['optionB'],
                $newData['optionC'],
                $newData['optionD']
            ];
            if (count(array_unique($options)) < 4) {
                $this->error('四个选项不能完全相同');
            }

            $newData['updateTime'] = time();

            if (QuestionModel::where(['id' => $id])->update($newData)) {
                $this->success('新增成功', null, '_parent_reload');
            } else {
                $this->error('新增失败');
            }
        }

        $formData = QuestionModel::find($id);
        return ZBuilder::make('form')
            ->setPageTitle('添加题目')
            ->setFormData($formData)
            ->addFormItems([
                ['textarea', 'topic', '题目'],
                ['text', 'optionA', '选项A'],
                ['text', 'optionB', '选项B'],
                ['text', 'optionC', '选项C'],
                ['text', 'optionD', '选项D'],
                ['text', 'correct', '答案'],
                ['select', 'activeId', '题目所属', '请选择这个题目所属的活动', $activeData],
            ])
            ->layout(['optionA' => 6, 'optionB' => 6, 'optionC' => 6, 'optionD' => 6, 'correct' => 6, 'activeId' => 6])
            ->fetch();
    }
}
